//
//  ZRRequestHint.m
//  Mcilife
//
//  Created by 黄涛 on 2019/4/18.
//  Copyright © 2019 syhzg. All rights reserved.
//

#import "ZRRequestHint.h"
#import <Masonry/Masonry.h>

@interface ZRRequestHint ()

@property (nonatomic , weak) UIButton * loadBtn;
@property (nonatomic , weak) UIImageView * logoImageView;
@property (nonatomic , copy) NSArray * hintLabelArray;

@end

@implementation ZRRequestHint

- (id)initWithView:(UIView *)view{
    
    if (self == [super initWithFrame:view.frame]) {
    
        CGRect frame = [view isKindOfClass:[UIScrollView class]] ?
        view.bounds:
        CGRectMake(0, ZRNavH, ZRScreenW, ZRScreenH);
        self.frame = frame;
        
        [view addSubview:self];
        self.state(nodata_ZRRequestState);
        [self masonryFrame];
    }
    return self;
}


#pragma mark - ---------------------- 生命周期 ---------------------------
#pragma mark - ---------------------- UI搭建  ---------------------------
/** ↓~ 视图布局 从这里设置哦 ~ ↓ */
- (void)masonryFrame{
    
    ZRWeakSelf
    [self.logoImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(APTX(125));
        make.centerX.equalTo(weakSelf);
        make.size.mas_equalTo(CGSizeMake(APTX(180), APTX(151)));
    }];
    
    [self.hintLabelArray mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(APTX(20));
        make.right.mas_equalTo(APTX(-20));
    }];
    
    [self layoutIfNeeded];
    CGFloat itemSpacing = 15;
    CGFloat topSpacing = 8;
    CGFloat itemHight = 20;
    CGFloat leadSpacing = self.logoImageView.maxY + topSpacing;
    CGFloat tailSpacing = self.height - weakSelf.logoImageView.maxY -topSpacing - itemSpacing - itemHight * 2;
    
    [self.hintLabelArray mas_distributeViewsAlongAxis:MASAxisTypeVertical withFixedSpacing:APTX(15) leadSpacing:APTX(leadSpacing) tailSpacing:APTX(tailSpacing)];
    
    UILabel * label = self.hintLabelArray.firstObject;
    [self.loadBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(label.mas_bottom).offset(APTX(0));
        make.centerX.mas_equalTo(weakSelf);
        make.size.mas_equalTo(CGSizeMake(APTX(100), APTX(30)));
        
    }];
    
}

#pragma mark - ---------------------- 数据操作 ---------------------------
#pragma mark - ---------------------- 其他方法 ---------------------------
#pragma mark - ---------------------- 响应事件 ---------------------------
#pragma mark - ---------------------- 代理方法 ---------------------------
#pragma mark - ---------------------- 活动监听 ---------------------------
#pragma mark - ------------------ getter 和 setter ----------------------
- (UIButton *)loadBtn{
    if (!_loadBtn) {
        
        UIButton * button = [[UIButton alloc] init];
        button.translatesAutoresizingMaskIntoConstraints = NO;
        button.titleLabel.font = [UIFont systemFontOfSize:18];
        button.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
        [button setTitleColor:[UIColor zr_colorWithHexString:@"#999999"] forState:UIControlStateNormal];
        [button setTitle:@"重新加载" forState:UIControlStateNormal];
        [button setImage:[UIImage imageNamed:@""] forState:UIControlStateNormal];
        [button setBackgroundImage:[UIImage imageNamed:@""] forState:UIControlStateNormal];
        [self addSubview:button];
        
        button.backgroundColor = ZRRandomColor;
        
        _loadBtn = button;
    }
    return _loadBtn;

}

- (UIImageView *)logoImageView{
    if (!_logoImageView) {
        
        UIImageView * imageView = [[UIImageView alloc] init];
        imageView.translatesAutoresizingMaskIntoConstraints = NO;
        imageView.image = [UIImage imageNamed:@""];
        imageView.contentMode = UIViewContentModeScaleAspectFill;
        [self addSubview:imageView];
        
        imageView.backgroundColor = ZRRandomColor;
        _logoImageView = imageView;
    }
    return _logoImageView;
    
}

- (NSArray *)hintLabelArray{
    if (!_hintLabelArray) {
        
        NSMutableArray * arr = [NSMutableArray array];
        
        for (int i = 0; i < 2; i++) {
            UILabel *  label = [[UILabel alloc] init];
            label.translatesAutoresizingMaskIntoConstraints = NO;
            label.text = i == 0 ? @"~ 这里空空如也 ~" : @"";
            label.textAlignment = NSTextAlignmentCenter;
            label.font = [UIFont systemFontOfSize:18];
            label.numberOfLines = 0;
            label.textColor = [UIColor zr_colorWithHexString:@"#999999"];
            [label sizeToFit];
            [self addSubview:label];
            
            label.backgroundColor = ZRRandomColor;
            [arr addObject:label];
            
        }
      
        _hintLabelArray = [[NSArray alloc] initWithArray:arr];
        
    }
    return _hintLabelArray;
}


- (Set)title{ ZRWeakSelf
    
    return _title = ^(NSString * value){
        if (!value.hash) return weakSelf;
        UILabel * label = weakSelf.hintLabelArray.firstObject;
        label.text = value;
        
        return weakSelf;
    };
}

- (Set)subTitle{ ZRWeakSelf
    
     return _subTitle = ^(NSString * value){
        if (!value.hash) return weakSelf;
        UILabel * label = weakSelf.hintLabelArray.lastObject;
        label.text = value;
        
        return weakSelf;
    };
    
}

- (Set)image{ZRWeakSelf
    
    return _image = ^(NSString * value){
        
        weakSelf.logoImageView.image = [UIImage imageNamed:value];
        return weakSelf;
    };
    
}

- (Chose)state{ZRWeakSelf
    
    return _state = ^(ZRRequestState requestState){
        
        UILabel * lable = weakSelf.hintLabelArray.lastObject;
        lable.hidden = requestState == error_ZRRequestState ? YES : NO;
        weakSelf.loadBtn.hidden = requestState == error_ZRRequestState ? NO : YES;
        
        return weakSelf;
    };
    
}

- (Add)action{ZRWeakSelf
    
    return _action = ^(SEL loadAction){
        
        UIResponder *responder = weakSelf.superview;
        while ((responder = [responder nextResponder])){
            if ([responder isKindOfClass: [UIViewController class]]){
                
                [weakSelf.loadBtn addTarget:responder action:loadAction forControlEvents:UIControlEventTouchUpInside];
            }
        }

        return weakSelf;
    };
    
}
@end
